﻿
using System;
using LOLServer.Cache.Interface;
using LOLServer.DAO.Model;
using NetFrame;
using System.Collections.Generic;
using NetFrame.Util;

namespace LOLServer.Cache.Ipml
{
    public class UserCache : IUserCache
    { /// <summary>
      /// 用户ID和模型的映射表
      /// </summary> 
        Dictionary<int, UserModel> mIdToModel = new Dictionary<int, UserModel>();
        /// <summary>
        /// 帐号ID和角色ID之间的绑定
        /// </summary> 
        Dictionary<int, int> mAccIdToUid = new Dictionary<int, int>();
         
        Dictionary<int, UserToken> mIdToToken = new Dictionary<int, UserToken>();
        Dictionary<UserToken, int> mTokenToId = new Dictionary<UserToken, int>();
        public bool Create(UserToken token, string name)
        {
            UserModel um=new UserModel();
            um.UserName = name;
            um.UserId = Utils.GetRandomString(10, true, false, false, false).ToInt();

            return true;
        }

        public UserToken GetConnectUser(int userId)
        {
            return mIdToToken[userId];
        }

        public UserModel GetUser(int userId)
        {
            return mIdToModel[userId];
        }

        public UserModel GetUser(UserToken token)
        {
            if (!HasUser(token)) return null;

            return mIdToModel[mTokenToId[token]];
        }

        public UserModel GetUserByAccountId(int accountId)
        {
            return new UserModel();
        }
        /// <summary>
        /// 是否存在用户
        /// </summary>
        /// <param name="tokne"></param>
        /// <returns></returns>
        public bool HasUser(UserToken tokne)
        {
            return mTokenToId.ContainsKey(tokne);
        }

        public bool HasUserByAccountId(int accountId)
        {
            return mAccIdToUid.ContainsKey(accountId);
        }

        public void OffLine(UserToken token)
        {
            if (mTokenToId.ContainsKey(token))
            {
                if (mIdToToken.ContainsKey(mTokenToId[token]))
                {
                    mIdToToken.Remove(mTokenToId[token]);
                }
                mTokenToId.Remove(token);
            }
        }

        public UserModel OnLine(UserToken token, int userId)
        {
            mIdToToken.Add(userId, token);
            mTokenToId.Add(token, userId);
            return mIdToModel[userId];
        }
        /// <summary>
        /// 角色是否已经在线
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool OnLine(int userId)
        {
            return mIdToToken.ContainsKey(userId);
        }
    }
}
